package gsw.demo.pluggable.plugin;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.io.File;

import gsw.demo.pluggable.plugin_simple.HookTool;
import gsw.demo.pluggable.R;
import gsw.tool.pluggable.core.PluginManager;

public class ActivityPlugin extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plugin);
        init();

        Toast.makeText(ActivityPlugin.this, "请确认是否在Application中初始化PluginManager", Toast.LENGTH_SHORT).show();
    }

    private void init() {
        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.removeAPK) {
                    String packageName = "gsw.toolpluggabledemo2";
                    PluginManager.getInstance().deletePackage(packageName, 0);
                } else if (v.getId() == R.id.loadAPK) {
                    //模拟下载APK:从资产文件复制到私有目录
                    String apkName = "ToolPluggableDemo2.apk";
                    HookTool.extractAssets(ActivityPlugin.this, apkName);
                    String apkPath = getFilesDir().getAbsolutePath() + "/" + apkName;
                    File file = new File(apkPath);

                    //将插件APK安装到宿主指定目录下
                    int flag = PluginManager.getInstance().installPackage(file.getAbsolutePath());

                    String msg = flag == 0 ? "APK安装成功" : "APK安装失败";
                    Toast.makeText(ActivityPlugin.this, msg, Toast.LENGTH_SHORT).show();
                } else if (v.getId() == R.id.jump1) {
                    startPluginActivity("gsw.demopluggable2.plugin.ActivityStandard1");
//                    startPluginActivity("gsw.demopluggable2.plugin.ActivityStandard2");
//                    startPluginActivity("gsw.demopluggable2.plugin.ActvitySingleTop1");
//                    startPluginActivity("gsw.demopluggable2.plugin.ActvitySingleTop2");
//                    startPluginActivity("gsw.demopluggable2.plugin.ActvitySingleTask1");
//                    startPluginActivity("gsw.demopluggable2.plugin.ActvitySingleTask2");
//                    startPluginActivity("gsw.demopluggable2.plugin.ActvitySingleInstance1");
//                    startPluginActivity("gsw.demopluggable2.plugin.ActvitySingleInstance2");
//
//                    startPluginActivity("gsw.demopluggable2.plugin_simple.ActivityPluginSimple1");
//                    startPluginActivity("gsw.demopluggable2.plugin_simple.ActivityPluginSimple2");
                }
            }
        };
        findViewById(R.id.removeAPK).setOnClickListener(clickListener);
        findViewById(R.id.loadAPK).setOnClickListener(clickListener);
        findViewById(R.id.jump1).setOnClickListener(clickListener);
    }

    /**
     * 根据类名，打开插件的Activity
     *
     * @param name 插件APK的Activity的类名
     */
    private void startPluginActivity(String name) {
        //插件APK的包名
        String pluginPackagenmae = "gsw.toolpluggabledemo2";
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(pluginPackagenmae, name));
        startActivity(intent);
    }
}
